package com.back.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.back.entity.Log;
import com.back.mapper.LogDao;
import com.back.service.inte.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
public class LogController {
    @Autowired
    LogService logService;
    @Autowired
    LogDao logDao;

    @GetMapping("/api/log")
    SaResult getlog(@RequestParam("current") int current, @RequestParam("pagesize") int pagesize, HttpServletRequest request){
        try{
            StpUtil.checkLogin();
            HashMap<String, Object> map = new HashMap<>();
            map.put("total",logDao.queryNumOfUid());
            map.put("loglist",logDao.queryByUid((current-1)*pagesize,pagesize));
//            logService.add(Log.ok(request,"获取日志成功"));
            return SaResult.ok("获取日志成功").setData(map);
        }catch (NotLoginException e){
//            logService.add(Log.error(request,e.getMessage()));
            return SaResult.code(401).setMsg("未登录，请先登录");
        }catch (Exception e){
//            logService.add(Log.error(request,e.getMessage()));
            return SaResult.error("获取日志失败");
        }
    }
}
